﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Convert.DataAccess
{
    public class dbSaveBatch
    {
        public static void SaveBatch(DataTable dataTable, string destinationTableName, string dbConnectionString)
        {

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(dbConnectionString, SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.KeepIdentity))
                {
                    bulkCopy.BulkCopyTimeout = 120;
                    bulkCopy.DestinationTableName = destinationTableName;

                    //add default column mappings based on DataTable
                    foreach (DataColumn col in dataTable.Columns)
                        bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(col.ColumnName, col.ColumnName));

                    bulkCopy.WriteToServer(dataTable);
                }

                //clear the datatable for re-use
                dataTable.Clear();

        }
    }
}
